package com.jwaoo.chat.data.db;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;

import com.jwaoo.chat.Message;
import com.jwaoo.chat.UserInfo;
import com.jwaoo.chat.fragment.session.bean.SessionBean;
import java.util.List;

/**
 * Created by jwaoo on 2017/6/15.
 */
@Dao
public interface DBMsgDao {

    @Insert
    void insertUserInfo(UserInfo userInfo);

    @Update
    void updateUserInfo(UserInfo userInfo);

    @Query("SELECT * FROM userinfo WHERE id = :arg0")
    UserInfo queryUserInfo(int arg0);

    @Insert
    void insertSession(SessionBean session);

    @Insert
    void insertALLSession(List<SessionBean> sessions);

    @Update
    void updateSession(SessionBean sessionBean);

    @Update
    void updateAllSession(List<SessionBean> list);

    @Insert
    void insertMessage(Message message);

    @Insert
    void insertALLMessage(List<Message> messages);

    @Query("SELECT * FROM session")
    List<SessionBean> queryAllSession();

    @Query("SELECT * FROM session WHERE friend = :arg0")
    SessionBean querySessionUnReadCount(int arg0);

    @Query("SELECT * FROM message WHERE friend = :arg0 and timestamp < :arg1 ORDER BY timestamp DESC limit 10")
    List<Message> queryMessage(int arg0,long arg1);

    @Query("SELECT * FROM message WHERE friend = :arg0 ORDER BY timestamp DESC limit 10")
    List<Message> queryMessage(int arg0);

    @Query("SELECT * FROM message where msgid = :arg0")
    Message queryMesaage(int arg0);

    @Query("SELECT * FROM message WHERE friend = :arg0 and msgtype = :arg1 ORDER BY timestamp DESC")
    List<Message> queryMessageWithIMG(int arg0,int arg1);

    @Query("SELECT * FROM message WHERE friend = :arg0")
    List<Message> queryAllMessageByID(int arg0);

    @Query("SELECT * FROM message WHERE friend = :arg0 and state <> 1  ORDER BY timestamp DESC")
    List<Message> querySendFailedMessages(int arg0);

    @Query("SELECT * FROM message WHERE friend = :arg0 and isRead = 1")
    List<Message> queryAllUnRedMessage(int arg0);

    @Update
    void updateMessage(Message message);

    @Update
    void updateMessage(List<Message> messages);


    @Query("SELECT * FROM message WHERE friend = :arg0 and isRead = 1 ORDER BY timestamp DESC")
    List<Message> queryAllUnReadMessage(String arg0);

    @Update
    void updateReadState(List<Message> list);

    @Delete
    void deleteSession(SessionBean sessionBean);

    @Delete
    void deleteMessage(List<Message> messages);

}
